//课设LED广告 数据锁存-单路控制方案
#include <reg51.h>
#include <intrins.h>
#define u8 unsigned char
#define u16	unsigned int

sbit LE1=P1^0;    //锁存器 1
sbit LE2=P1^1;    //锁存器2

void delay_ms(u16 x)
{
	u8 t;
	while(x--){
		for(t=0;t<100;t++);
	}
}


void row_data_send(u16 dat){
// 锁存器数据传输
	int delay=5;
	u8 temp;
	temp=dat>>8; // 传入高字节
	LE2=1;
	//_nop_();_nop_();
	P0=temp;
	delay_ms(delay);
	LE2=0;
	delay_ms(delay);
	temp=dat; // 传入低字节
	LE1=1;
	//_nop_();_nop_();
	P0=temp;
	delay_ms(delay);
	LE1=0;
	delay_ms(delay);
}


void row_data_send_switch(u16 dat){
// 锁存器数据传输 单路控制方案
	int delay=1;
	u8 temp;
	temp=dat>>8; // 传入高字节
	LE1=0;
	P0=temp;
	temp=dat; // 传入低字节
	delay_ms(delay);
	LE1=1;
	//delay_ms(delay);
	P0=temp;
	delay_ms(delay);
}

void main(){
	u8 out_side;

	//u16 test[16] = {0x00F0,0x0F00,0xF000,0x000F,0x0006,0x0060,0x0600,0x6000,0x00F0,0x0F00,0xF000,0x000F,0x0006,0x0060,0x0600,0x6000};
	//u16 T_zhang[16] = {0xFCF9,0x9CF1,0xCCE3,0xCCC7,0xE413,0xF07B,0xF4F9,0x0000,0x0001,0xFCFF,
	//0x0303,0x0301,0xCCC3,0xCCC3,0x303C,0x383C};
	u16 T_zhang[16] = {0x0306,0x630E,0x331C,0x3338,0x1BEC,0x0F84,0x0B06,0xFFFF,0xFFFE,0x0300,
	0xFCFC,0xFCFE,0xCCC3,0xCCC3,0xCFC3,0xC7C3};

	u16* TAB_addr[1] = {&T_zhang}; // 存放阵列阴极行码数组地址
	u16 *TAB; // 阳极码数组指针
	int delay_col =1;
	int i;
	int num;
	int time;
	LE1=1;
	while (1){
		for(num=0;num<1;num++){
			TAB = TAB_addr[num];  //选用阴极行码数组
			for(time=0;time<50;time++){
				out_side = 0x00; //阳极 列(低位在右) 
				for(i=0;i<16;i++){
					P2=out_side;
					//row_data_send_switch(*(TAB+4));	//阴极,行(高位在上)
					row_data_send_switch(*(TAB+i));	//阴极,行(高位在上)
					out_side++;
					delay_ms(delay_col);
				}
				P0=0x00;
				LE1=1;
				LE1=0;	
			}
			P2=0x00; // 消隐
		}
	}
}
